package com.example.pdr;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;

import java.util.ArrayList;

public class TrackingView extends View {
    private final Paint paint;
    private final ArrayList<Float> pathX = new ArrayList<>();
    private final ArrayList<Float> pathY = new ArrayList<>();

    private float centerX;
    private float centerY;

    public TrackingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        paint.setColor(0xFF0000FF); // 蓝色
        paint.setStrokeWidth(5);
    }

    //未调用该函数
    public void addCenterPoint(int width, int height) {
        centerX = width / 2f;
        centerY = height / 2f;
        pathX.add(centerX);
        pathY.add(centerY);
        invalidate();
    }

    public void addPoint(float x, float y, int width, int height) {
        centerX = width / 2f;
        centerY = height / 2f;

        float normalizedX = (x / 100) * width + centerX;
        float normalizedY = (y / 100) * height + centerY;

        pathX.add(normalizedX);
        pathY.add(normalizedY);
        invalidate();
    }

    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        super.onDraw(canvas);
        for (int i = 0; i < pathX.size() - 1; i++) {
            canvas.drawLine(pathX.get(i), pathY.get(i), pathX.get(i + 1), pathY.get(i + 1), paint);
        }
    }
}